package com.springcloudalibaba.serviceJournal.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springcloudalibaba.bclass.base.BasePageResult;
import com.springcloudalibaba.bclass.base.BaseResult;
import com.springcloudalibaba.bclass.util.StrUtil;
import com.springcloudalibaba.serviceJournal.entity.LogEntity;
import com.springcloudalibaba.serviceJournal.service.LogService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 系统日志 前端控制器
 * </p>
 *
 * @author lixing_java@163.com
 * @since 2021-06-06
 */
@RestController
@RequestMapping("/log")
public class LogController {

    @Resource
    private LogService logService;

    /**
     * @描述 搜索条件
     * @作者 lixing
     * @日期 2021/6/6 10:40
     */
    public QueryWrapper<LogEntity> queryWrapper(LogEntity entity) {
        QueryWrapper<LogEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        //        if (StrUtil.isEffective(entity.getCreateUserAccount())) { // TODO 根据当前登陆的管理员账号筛选数据
        //            queryWrapper.eq("create_user_account", entity.getCreateUserAccount());
        //        }
        //        if (entity.getCreateUserGrade() != null) { // TODO 根据当前登陆的管理员等级筛选数据
        //            if (entity.getCreateUserGrade() == 2) {
        //                queryWrapper.in("create_user_grade", 2, 3, 4);
        //            } else if (entity.getCreateUserGrade() == 3) {
        //                queryWrapper.in("create_user_grade", 3, 4);
        //            } else if (entity.getCreateUserGrade() == 4) {
        //                queryWrapper.eq("create_user_grade", 4);
        //            }
        //        }
        if (entity.baseCreateTimeStart != null) {
            queryWrapper.ge("create_time", entity.baseCreateTimeStart);
        }
        if (entity.baseCreateTimeEnd != null) {
            queryWrapper.le("create_time", entity.baseCreateTimeEnd);
        }
        if (entity.getCategory() != null) {
            queryWrapper.eq("category", entity.getCategory());
        }
        if (StrUtil.isEffective(entity.getContent())) {
            queryWrapper.like("content", entity.getContent());
        }
        return queryWrapper;
    }

    /*
     * @描述 TODO 分页列表
     * @作者 lixing
     * @日期 2020/9/7 21:20
     */
    @PostMapping("pageLog")
    public BasePageResult<LogEntity> pageLog(@RequestBody LogEntity entity) {
        Page<LogEntity> page = logService.page(new Page<>(entity.getBasePageNum(),entity.getBasePageSize()), queryWrapper(entity));
        return new BasePageResult<>(page);
    }

    /*
     * @描述 TODO 添加
     * @作者 lixing
     * @日期 2020/9/7 22:08
     */
    @PostMapping("insertLog")
    public BaseResult<Object> insertLog(@RequestBody LogEntity entity) {
        boolean flag = logService.save(entity);
        return flag ? new BaseResult<Object>().success(entity.getId()) : new BaseResult<Object>().fail("添加失败");
    }

    /*
     * @描述 TODO 详情
     * @作者 lixing
     * @日期 2019/9/11 9:14
     */
    @PostMapping("detailLog")
    public BaseResult<LogEntity> detailLog(@RequestBody LogEntity entity) {
        entity = logService.getById(entity.getId());
        return new BaseResult<LogEntity>().success(entity);
    }

    /*
     * @描述 TODO 删除
     * @作者 lixing
     * @日期 2019/9/11 9:14
     */
    @PostMapping("deleteLog")
    public BaseResult<Boolean> deleteLog(@RequestBody LogEntity entity) {
        boolean flag;
        if (!entity.getBaseLongList().isEmpty()) {
            flag = logService.removeByIds(entity.getBaseLongList());
        } else if (null != entity.getId()) {
            flag = logService.removeById(entity.getId());
        } else {
            return new BaseResult<Boolean>().fail("主键缺失");
        }
        return flag ? new BaseResult<Boolean>().success() : new BaseResult<Boolean>().fail();
    }
}

